package cn.easybuy.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.easybuy.dao.BaseDao;
import cn.easybuy.dao.OrderDao;
import cn.easybuy.entity.Order;

public class OrderDaoImpl extends BaseDao implements OrderDao{

	@Override
	public List<Order> getOrdersList() {
		String sql="select * from easybuy_order";
		ResultSet rs=this.exxcuteQuery(sql);
		List<Order> list=new ArrayList<Order>();
		try {
			while (rs.next()) {
				Order order=new Order();
				order.setId(rs.getInt(1));
				order.setUserId(rs.getInt(2));
				order.setLoginName(rs.getString(3));
				order.setUserAddress(rs.getString(4));
				order.setCreateTime(rs.getDate(5));
				order.setCost(rs.getDouble(6));
				order.setSerialNumber(rs.getString(7));
				list.add(order);
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(connection, pstmt, rs);
		}
		return list;
	}

	@Override
	public int save(Order order) {
		String sql="insert into easybuy_order(userId,loginName,userAddress,createTime,cost,serialNumber) values(?,?,?,?,?,?)";
		int num=this.executeUpdate(sql, order.getUserId(),order.getLoginName(),order.getUserAddress(),order.getCreateTime(),order.getCost(),order.getSerialNumber());
		return num;
	}

	@Override
	public int getIdNewByUserId(int userId) {
		String sql="SELECT id FROM easybuy_order WHERE userId = ? ORDER BY createTime DESC LIMIT 1";
		int id=0;
		ResultSet rs=this.exxcuteQuery(sql, userId);
		try {
			while (rs.next()) {
				id=rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(connection, pstmt, rs);
		}
		return id;
	}
	

	

}
